require "Net/send_Msg"
require 'View/UIBase/UIBase'

UIFriendCommunication = UIBase.New(PanelNames.UIFriendCommunication);
function UIFriendCommunication.Init()
    UIBase.Init(UIFriendCommunication);
end

--定义控件
function UIFriendCommunication:OnCreate()
    --table
    --飞飞加成数据
    self.recordCfg = {};
    --list
    --战斗信息列表
    self.recordList = self:GetChild("InformationList");
    self.recordList:SetVirtual()
    --btn
    self.backBtn = self:GetChild("btnBack");
    self.bgClose = self:GetChild("bgClose");
    --controller
    --是否有战报信息
    self.isHaveCtrl = self:GetController("isHave");

    --初始化职业加成列表数据
    self:addEvent();
end

function UIFriendCommunication:OnEnable()
    friendHandler.ReqFriendCompeteHistory()

end

function UIFriendCommunication:OnDisable()

end

function UIFriendCommunication:OnDestroy()

end

function UIFriendCommunication:addEvent()
    --btnChangeFormation.onClick:Add(function()
    --    self.ShowSelectFom(1);
    --end);
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.bgClose, self.onCloseClick);
    self:RegisterEvent(Config.EventType.FriendCompeteHistory, self.init, self)

end

function UIFriendCommunication:removeEvent()
    self:UnRegisterEvent(Config.EventType.FriendCompeteHistory, self.init, self);

end

--初始化
----初始化战报列表
function UIFriendCommunication:init(msg)
    self.recordCfg = msg.list;
    if #self.recordCfg <= 0 then
        self.isHaveCtrl.selectedIndex = 1;
    else
        self.isHaveCtrl.selectedIndex = 0;
        self.recordList.itemRenderer = self.rendererShow;
        self.recordList.numItems = #self.recordCfg;
    end
end

--战报列表
function UIFriendCommunication.rendererShow(index, obj)
    local self = UIFriendCommunication
    local idx = index + 1;
    local data = self.recordCfg[idx].record;
    local nameText = obj:GetChild("Name");--对手姓名
    local head = obj:GetChild("Head");--对手头像
    local time1 = obj:GetChild("Year") --日期
    local Level = obj:GetChild("Level")--好友等级
    --local time2 = obj:GetChild("time") --時間
    local headFrame = obj:GetChild("headframe");--对手头像框
    local ctrlActOrDef = obj:GetController("c1")--0攻击 1 被攻击
    local ctrlWinOrFail = obj:GetController("c2")-- 0失败 1胜利
    local ctrlIsFriend = obj:GetController("c3") -- 是否是好友控制器 0陌生人 1好友
    local btn_revenge = obj:GetChild("btn_revenge")--复仇
    local btn_playRecord = obj:GetChild("btn_playRecord")--播放

    --默认攻击者是玩家
    local myInfo = data.attacker;
    local otherInfo = data.defender;
    ctrlActOrDef:SetSelectedIndex(0);
    ctrlWinOrFail:SetSelectedIndex(data.win == 1 and 0 or 1);
    if tostring(data.attacker.playerId) ~= logicMgr.HeroManager.GetRoleInfo().playerId then
        --攻击者不是玩家
        myInfo = data.defender;
        otherInfo = data.attacker;
        ctrlActOrDef:SetSelectedIndex(1);
        --ctrlWinOrFail:SetSelectedIndex(data.win == 1 and 1 or 0);
    end
    local timeList = getTimeStrType2(tonumber(tostring(data.createTime)));
    nameText.text = otherInfo.playerName;
    head:GetChild("Icon").url = logicMgr.SkinManager.GetHeroIconBySkinId(tonumber(otherInfo.pic))
    headFrame.url = "ui://ICONAtlas/" .. otherInfo.picFrame;
    Level.text = self.recordCfg[idx].lv;
    time1.text = timeList;
    --time2.text = timeList.hour .. "." .. timeList.minute .. "." .. timeList.second;
    if FriendManager.GetMyFriend(otherInfo.playerId) == nil then
        ctrlIsFriend:SetSelectedIndex(0);
        btn_revenge.onClick:Clear();
        btn_revenge.onClick:Add(function()
            UIFriendCommunication:onClickRevenge(otherInfo.playerId);
        end);
    else
        ctrlIsFriend:SetSelectedIndex(1);
    end
    btn_playRecord.onClick:Clear();
    btn_playRecord.onClick:Add(function()
        --播放战报
        self:RequestFightCmd(data)
    end);
end

-------------------------------按钮监听
---关闭页面
function UIFriendCommunication:onCloseClick()
    closeUI(PanelNames.UIFriendCommunication);
end

--- 反击
function UIFriendCommunication:onClickRevenge(friendId)
    friendHandler.ReqFriendCompeteInfo(friendId)
end
---播放战报


function UIFriendCommunication:RequestFightCmd(record)
    self.autoPlayback = true;

    if self.requesting then
        return ;
    end
    self.requesting = true;
    self.record = record;
    --- 请求战斗命令
    BattlePlaybackManager.requestFightCmd(self.record, EnumConst.FightTypeConst.FriendCompeteBattle, function(fightCmdList)
        self.requesting = false;
        self.fightCmdList = fightCmdList;

        if self.destroyed then
            return ;
        end

        if self.autoPlayback then
            self:Playback();
        end
    end);
end

function UIFriendCommunication:Playback()
    if self.fightCmdList == nil then
        return ;
    end

    self.autoPlayback = false;

    BattlePlaybackManager.setBattleRecord(self.record);
    BattlePlaybackManager.setBattleResult(self.record.win);
    if BattlePlaybackManager.getBattleResult() == 1 then
        logicMgr.FubenModel.SetPassTimeForLast(math.floor(HelperFunc.GetServerTime()))
    end
    BattlePlaybackManager.playBattle(self.fightCmdList, EnumConst.FightTypeConst.FriendCompeteBattle, function()
        FightManager.SetRePlay(true);
        UIBottom.closeAllUI();
        closeUI(PanelNames.UIFriendCommunication);
    end, function()
        showUI(PanelNames.UIFriend);
        showUI(PanelNames.UIFriendCommunication);
    end);
end

return UIFriendCommunication;